package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import com.mars.framework.context.ContextUserInfoThreadHolder;
import com.mars.module.admin.entity.SysScenicArea;
import com.mars.module.admin.entity.SysScenicSpot;
import com.mars.module.system.entity.SysUser;
import com.mars.module.system.mapper.SysUserMapper;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.SysUserRechargeRecordRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.SysUserRechargeRecordMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.SysUserRechargeRecord;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.ISysUserRechargeRecordService;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 用户充值记录业务层处理
 *
 * @author mars
 * @date 2024-03-26
 */
@Slf4j
@Service
@AllArgsConstructor
public class SysUserRechargeRecordServiceImpl implements ISysUserRechargeRecordService {

    private final SysUserRechargeRecordMapper baseMapper;
    private final SysUserMapper userMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public SysUserRechargeRecord add(SysUserRechargeRecordRequest request) {
        SysUserRechargeRecord entity = SysUserRechargeRecord.builder().build();
        BeanUtils.copyProperties(request, entity);
        Long userId = ContextUserInfoThreadHolder.get().getId();
        entity.setCreateById(userId);
        baseMapper.insert(entity);
        SysUser sysUser = userMapper.selectById(userId);
        sysUser.setBalance(sysUser.getBalance() + request.getMoney());
        userMapper.updateById(sysUser);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Long> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(SysUserRechargeRecordRequest request) {
        SysUserRechargeRecord entity = SysUserRechargeRecord.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0;
    }

    @Override
    public SysUserRechargeRecord getById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<SysUserRechargeRecord> pageList(SysUserRechargeRecordRequest request) {
        Page<SysUserRechargeRecord> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<SysUserRechargeRecord> query = this.buildWrapper(request);
        IPage<SysUserRechargeRecord> pageRecord = baseMapper.selectPage(page, query);
        List<SysUserRechargeRecord> records = pageRecord.getRecords();
        List<SysUserRechargeRecord> recordsNew = new ArrayList<>();
        for (SysUserRechargeRecord record : records) {
            SysUser sysUser = userMapper.selectById(record.getUserId());
            record.setUserName(sysUser == null ? "" : sysUser.getUserName());
            recordsNew.add(record);
        }
        pageRecord.setRecords(recordsNew);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<SysUserRechargeRecord> list(SysUserRechargeRecordRequest request) {
        LambdaQueryWrapper<SysUserRechargeRecord> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<SysUserRechargeRecord> buildWrapper(SysUserRechargeRecordRequest param) {
        LambdaQueryWrapper<SysUserRechargeRecord> query = new LambdaQueryWrapper<>();
        if (param.getMoney() != null) {
            query.eq(SysUserRechargeRecord::getMoney, param.getMoney());
        }
        if (param.getUserId() != null) {
            query.eq(SysUserRechargeRecord::getUserId, param.getUserId());
        }
        return query;
    }

}
